﻿using System;
using System.Collections.Generic;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Web.MVP.Presentation;

namespace Medianamik.UI.Web.Shop.Presentation.Presenters
{
    public class EditAttributeTypePresenter<TView>: NodeTypeEditModulePresenter<TView>
        where TView : class, IEditAttributeTypeView
    {
        public EditAttributeTypePresenter(TView view)
            : base(view)
        {
        }

        public override Guid? ParentTypeId
        {
            get
            {
                return base.ParentTypeId ?? TypeIds.AttributeTypeBase;
            }
        }

        private Guid _newPropertyId;

        public override string ReturnUrl
        {
            get
            {
                return base.ReturnUrl ?? Pages.AttributeTypeValueList;
            }
        }

        protected override NodeType NewNodeType()
        {
            var type = base.NewNodeType();

            var property = type.AddProperty(Constants.DefaultAttributeTypeValuePropertyName,
                    250,
                    null,   // minLength
                    false,  // haveTooltip
                    !View.ValuesAreLocalized,
                    false,  // allowNulls
                    true,   // isUnique
                    null,   // validationRegex
                    "Content",
                    MedianamikControls.SingleLineTextBox,
                    new MedianamikControlInfo(), 
                    DataTypes.Varchar,
                    null,   // primaryTypeID
                    null,   // primaryMedianamikControlID
                    null,   // primaryMedianamikControlSettings
                    null,   // primaryDataTypeId
                    DataTypes.UniqueIdentifier,
                    null    // treeRootNodeId
                );
            property.IsRequired = true;

            _newPropertyId = property.ID;
            
            return type;
        }

        protected override void SaveNodeType()
        {
            base.SaveNodeType();

            Translator.CreateResource("TypeManagement", _newPropertyId + "_Label", new Dictionary<string, string> { { "en-CA", "Value" }, { "fr-CA", "Valeur" } });
        }
    }
}